package net.ojbk.jiaofu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import net.ojbk.jiaofu.service.StudentService;
import net.ojbk.jiaofu.service.TaskService;

/**
 * Servlet implementation class GetJsonServlet
 */
@WebServlet("/GetJsonServlet")
public class GetJsonServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
	    response.setCharacterEncoding("utf-8");
	    PrintWriter out = response.getWriter();  
		//map
	    //String taskId=request.getParameter("taskId");
	    HttpSession session=request.getSession();
	    String taskId=(String)session.getAttribute("taskId");
	    System.out.println(taskId);
	    //String taskId="ESKDYK";
		String time[]=TaskService.getSubmitTime(taskId);
		int num[]=TaskService.getSubmitNum(taskId);
		String userId[]=StudentService.outstandingPerson(taskId);
		int userFail = userId.length;
		int allCount = TaskService.getCountByTaskId(taskId);
		int userWin=allCount-userFail;
		
	    Map<String,Object>map=new HashMap<String,Object>();
		map.put("time", time);
		map.put("num", num);
		map.put("userWin", userWin);
		map.put("userFail", userFail);	
		JSONObject json=new JSONObject(map);
		String jsonString=json.toString();
		out.write(json.toString());
		out.flush();
		System.out.println(json.toString());
	}

}
